package com.mMap.controller;

import com.mMap.entity.constants.Constants;
//import com.mMap.entity.dto.SessionShareDto;
import com.mMap.entity.dto.SessionWebUserDto;
import com.mMap.entity.enums.ResponseCodeEnum;
import com.mMap.entity.vo.PaginationResultVO;
import com.mMap.entity.vo.ResponseVO;
//import com.mMap.utils.CopyTools;
//import com.mMap.utils.StringTools;
import com.mMap.utils.CopyTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

//@CrossOrigin(origins = "*",allowCredentials = "true")
public class ABaseController {

    private static final Logger logger = LoggerFactory.getLogger(ABaseController.class);

    protected static final String STATUC_SUCCESS = "success";

    protected static final String STATUC_ERROR = "error";

    protected <T> ResponseVO getSuccessResponseVO(T t) {
        ResponseVO<T> responseVO = new ResponseVO<>();
        responseVO.setStatus(STATUC_SUCCESS);
        responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
        responseVO.setInfo(ResponseCodeEnum.CODE_200.getMsg());
        responseVO.setData(t);
        return responseVO;
    }
    protected <T> ResponseVO getSuccessResponseVO(T t,String msg) {
        ResponseVO<T> responseVO = new ResponseVO<>();
        responseVO.setStatus(STATUC_SUCCESS);
        responseVO.setCode(ResponseCodeEnum.CODE_200.getCode());
        responseVO.setInfo(msg);
        responseVO.setData(t);
        return responseVO;
    }
    protected <T> ResponseVO getErrorResponseVO(T t,String msg) {
        ResponseVO<T> responseVO = new ResponseVO<>();
        responseVO.setStatus(STATUC_ERROR);
        responseVO.setCode(ResponseCodeEnum.CODE_201.getCode());
        responseVO.setInfo(msg);
        responseVO.setData(t);
        return responseVO;
    }

    protected <S, T> PaginationResultVO<T> convert2PaginationVO(PaginationResultVO<S> result, Class<T> classz) {
        PaginationResultVO<T> resultVO = new PaginationResultVO<>();
        resultVO.setList(CopyTools.copyList(result.getList(), classz));
        resultVO.setPageNo(result.getPageNo());
        resultVO.setPageSize(result.getPageSize());
        resultVO.setPageTotal(result.getPageTotal());
        resultVO.setTotalCount(result.getTotalCount());
        return resultVO;
    }
//
    protected SessionWebUserDto getUserInfoFromSession(HttpSession session) {
        System.out.println("session:"+session.getId());
        SessionWebUserDto sessionWebUserDto = (SessionWebUserDto) session.getAttribute(Constants.SESSION_KEY);
        return sessionWebUserDto;
    }
//
//
//    protected SessionShareDto getSessionShareFromSession(HttpSession session, String shareId) {
//        SessionShareDto sessionShareDto = (SessionShareDto) session.getAttribute(Constants.SESSION_SHARE_KEY + shareId);
//        return sessionShareDto;
//    }
//
//
//    protected void readFile(HttpServletResponse response, String filePath) {
//        if (!StringTools.pathIsOk(filePath)) {
//            return;
//        }
//        OutputStream out = null;
//        FileInputStream in = null;
//        try {
//            File file = new File(filePath);
//            if (!file.exists()) {
//                return;
//            }
//            in = new FileInputStream(file);
//            byte[] byteData = new byte[1024];
//            out = response.getOutputStream();
//            int len = 0;
//            while ((len = in.read(byteData)) != -1) {
//                out.write(byteData, 0, len);
//            }
//            out.flush();
//        } catch (Exception e) {
//            logger.error("读取文件异常", e);
//        } finally {
//            if (out != null) {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    logger.error("IO异常", e);
//                }
//            }
//            if (in != null) {
//                try {
//                    in.close();
//                } catch (IOException e) {
//                    logger.error("IO异常", e);
//                }
//            }
//        }
//    }
}
